<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>超市账单管理系统</title>
    <link rel="stylesheet" href="css/public.css"/>
    <link rel="stylesheet" href="css/style.css"/>
    <link rel="stylesheet" href="js/bootstrap-3.3.7-dist/css/bootstrap.css"/>
    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript" src="js/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
    <script type="text/javascript" src="js/vue.js"></script>
    <script type="text/javascript" src="js/axios.min.js"></script>
</head>
<body>
<!--头部-->
    <header class="publicHeader">
        <h1>超市账单管理系统</h1>
        <div class="publicHeaderR">
            <p><span>下午好！</span><span style="color: #fff21b"> Admin</span> , 欢迎你！</p>
            <a href="login.jsp">退出</a>
        </div>
    </header>
<!--时间-->
    <section class="publicTime">
        <span id="time">2015年1月1日 11:11  星期一</span>
        <a href="#">温馨提示：为了能正常浏览，请使用高版本浏览器！（IE10+）</a>
    </section>
<!--主体内容-->
    <section class="publicMian ">
        <div class="left">
            <h2 class="leftH2"><span class="span1"></span>功能列表 <span></span></h2>
            <nav>
                <ul class="list">
                    <li><a href="billList.html">账单管理</a></li>
                    <li><a href="providerList.html">供应商管理</a></li>
                    <li  id="active"><a href="userList.html">用户管理</a></li>
                    <li><a href="password.html">密码修改</a></li>
                    <li><a href="login.html">退出系统</a></li>
                </ul>
            </nav>
        </div>
        <!--vue挂载的dom元素为class=“right”，取一个id="app"-->
        <div class="right" id="app">
            <div class="location">
                <strong>你现在所在的位置是:</strong>
                <span>用户管理页面</span>
            </div>
            <div class="search">
                <span>用户名：</span>
                <input type="text" placeholder="请输入用户名"/>
                <input type="button" value="查询"/>
                <a href="userAdd.html">添加用户</a>
            </div>
            <!--用户-->
            <table class="providerTable" cellpadding="0" cellspacing="0">
                <tr class="firstTr">
                    <th width="10%">用户编码</th>
                    <th width="20%">用户名称</th>
                    <th width="10%">性别</th>
                    <th width="10%">年龄</th>
                    <th width="10%">电话</th>
                    <th width="10%">用户类型</th>
                    <th width="30%">操作</th>
                </tr>
                <tr v-for="obj,key in userList">
                    <td>{{obj.userCode}}</td>
                    <td>{{obj.userName}}</td>
                    <td>{{obj.gender==1?'女':'男'}}</td>
                    <td>{{obj.birthday}}</td>
                    <td>{{obj.phone}}</td>
                    <td>{{obj.userRole.roleName}}</td>
                    <td>
                        <a href="userView.html"><img src="img/read.png" alt="查看" title="查看"/></a>
                        <a href="#" @click.prevent="findById(obj.id)"><img src="img/xiugai.png" alt="修改" title="修改"/></a>
                        <a href="#" @click.prevent="del(obj.id,key)" class="removeUser"><img src="img/schu.png" alt="删除" title="删除"/></a>
                    </td>
                </tr>
            </table>
            <!--模态框，显示，修改，删除功能-->

            <!-- Modal -->
            <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                            <h4 class="modal-title" id="myModalLabel">Modal title</h4>
                        </div>
                        <div class="modal-body">
                            <div class="providerAdd">
                                <form action="#">
                                    <!--div的class 为error是验证错误，ok是验证成功-->
                                    <div>
                                        <label for="userName">用户名称：</label>
                                        <input type="text" name="userName" id="userName" v-model="user.userName"/>
                                        <span >*</span>
                                    </div>

                                    <div>
                                        <label >用户性别：</label>

                                        <select name="" v-model="user.gender">
                                            <option value="2" >男</option>
                                            <option value="1" >女</option>
                                        </select>
                                    </div>
                                    <div>
                                        <label for="data">出生日期：</label>
                                        <input type="text" name="data" id="data" v-model="user.birthday"/>
                                        <span >*</span>
                                    </div>
                                    <div>
                                        <label for="userphone">用户电话：</label>
                                        <input type="text" name="userphone" id="userphone" v-model="user.phone"/>
                                        <span >*</span>
                                    </div>
                                    <div>
                                        <label for="userAddress">用户地址：</label>
                                        <input type="text" name="userAddress" id="userAddress" v-model="user.address"/>
                                    </div>
                                    <div>
                                        <label >用户类别：</label>
                                        <input type="radio" name="userlei" value="1" v-model="user.userRole.id"/>管理员
                                        <input type="radio" name="userlei" value="2" v-model="user.userRole.id"/>经理
                                        <input type="radio" name="userlei" value="3" v-model="user.userRole.id"/>普通用户

                                    </div>
                                </form>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal" @click="updateUser">保存</button>
                            <button type="button" class="btn btn-primary" data-dismiss="modal">返回</button>
                        </div>
                    </div>
                </div>
            </div>

        </div>
        <!--vue挂载元素结束位置-->
    </section>

    <footer class="footer">
        版权归北大青鸟
    </footer>

<script src="js/time.js"></script>

    <script type="text/javascript">
        //实例化vue对象
        new Vue({
            el:"#app",
            data:{
                userList:[],//用于存储用户列表
                user:{userRole:{id:""}},//用于存储单个的用户信息
            },
            methods:{
                //查询所有
                findAll:function () {
                    var _this=this;
                    //给userList赋值
                    //发送axios获取数据
                    axios.get('/userList.do')
                        .then(function (response) {
                            //获取后台返回的数据
                            console.log(response.data);
                            _this.userList=response.data;
                        })
                        .catch(function (error) {
                            console.log(error);
                        });
                },
                //根据id查看个人信息
                findById:function(userId){
                    var _this=this;
                    //给user赋值
                    axios.get('/userUpdateView.do',{
                        params:{
                            id:userId
                        }
                    }).then(function (response) {
                        console.log(response);
                        _this.user=response.data;

                    })
                        .catch(function (error) {
                            console.log(error)
                        })
                    //打开模态框
                    $('#myModal').modal('show')
                },
                del:function (userId,index) {
                    var _this=this;
                    //后台根据id删除
                    //前台userList.splice(index,1)
                    if (confirm("确认要删除吗？")){
                        axios.get('/userDelete.do',{
                            params:{
                                id:userId
                            }
                        }).then(function (response) {
                            _this.userList.splice(index,1);
                        })
                        .catch(function (error) {
                            console.log(error)
                        })
                    }
                },
                updateUser:function () {
                    //发送axios，请求后台修改数据
                    var _this=this;
                    axios.post('/userUpdate.do',_this.user).then(function (response) {
                        //调用findAll更新数据
                        _this.findAll();
                    })
                        .catch(function (error) {
                            console.log(error)
                        })


                }
            },
            //实例化vue对象之后调用findAll方法
            created:function () {
                this.findAll();
            }
        })

    </script>

</body>
</html>